package com.shop.listener;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionIdListener;
import javax.servlet.http.HttpSessionListener;
//@WebListener
public class MySessionListener 
 implements HttpSessionListener,HttpSessionIdListener ,HttpSessionAttributeListener{
	public MySessionListener(){
		System.out.println("===创建MySessionListener===");
	}
	
	 public void sessionCreated(HttpSessionEvent se){
		 HttpSession session = se.getSession();
		 System.out.println("创建session:" + session.getId());
	 }
	   
	 public void sessionDestroyed(HttpSessionEvent se){
		 int maxInactiveInterval = se.getSession().getMaxInactiveInterval();
		 long lastAccessedTime = se.getSession().getLastAccessedTime();
		 Date date = new Date(lastAccessedTime);
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 System.out.println("session非活动时间间隔："+maxInactiveInterval);
		 System.out.println("session最后访问时间:"+sdf.format(date));
		 System.out.println("session销毁ID=="+se.getSession().getId());
	 }

	@Override
	public void sessionIdChanged(HttpSessionEvent event, String oldSessionId) {
		System.out.println("===sessionIdChanged===");
		String id = event.getSession().getId();
		System.out.println("newSessionId==="+id);
		System.out.println("oldSessionId===" + oldSessionId);
		
		
	}
}
